﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using com.dz.entity;
using com.dz.common;
using System.Data;
namespace com.dz.dataaccess
{
    public class UserDao
    {
        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="i_user_name"></param>
        /// <param name="i_user_password"></param>
        /// <param name="i_user_email"></param>
        /// <returns></returns>
        public long RegisterUser(string i_user_name,string i_user_password,string i_user_email,string i_nick_name)
        {
           
            try
            {
                int rowsAffected;
                SqlParameter[] parameters = {
					new SqlParameter("@userId", SqlDbType.BigInt,8),
					new SqlParameter("@userName", SqlDbType.NVarChar,100),
					new SqlParameter("@password", SqlDbType.NVarChar,50),
					new SqlParameter("@email", SqlDbType.NVarChar,50),
                    new SqlParameter("@nickName",SqlDbType.NVarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = i_user_name;
                parameters[2].Value = i_user_password;
                parameters[3].Value = i_user_email;
                parameters[4].Value = i_nick_name;
                DbHelperSQL.RunProcedure("proc_tbUser_Add", parameters, out rowsAffected);
                return (long)parameters[0].Value;
            }
            catch (Exception ex)
            {
                throw new DataException("创建用户出现异常", ex);
            }
        }

        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="i_user_name"></param>
        /// <param name="i_user_password"></param>
        /// <returns></returns>
        public DataSet LoginUser(string i_user_name, string i_user_password)
        {
            try
            {
                SqlParameter[] parameters = {
					new SqlParameter("@userName", SqlDbType.NVarChar,50),
                    new SqlParameter("@password", SqlDbType.NVarChar,50)
					};
                parameters[0].Value = i_user_name;
                parameters[1].Value = i_user_password;

               DataSet ds= DbHelperSQL.RunProcedure("proc_tbUser_Login", parameters,"ds");
               return ds;
            }
            catch (Exception ex)
            {
                throw new DataException("用户登陆异常", ex);
            }
        }

        /// <summary>
        /// 获取用户评论
        /// </summary>
        /// <param name="i_user_id"></param>
        /// <param name="i_page_index"></param>
        /// <param name="i_page_size"></param>
        /// <param name="o_total_count"></param>
        /// <returns></returns>
        public DataSet GetUserReview(long i_user_id,int i_page_index,int i_page_size,out int o_total_count)
        {
            o_total_count = 0;
            try
            {
                SqlParameter[] parameters = {
					new SqlParameter("@userId", SqlDbType.BigInt,8),
                    new SqlParameter("@pageIndex", SqlDbType.Int,4),
                     new SqlParameter("@pageSize", SqlDbType.Int,4),
                      new SqlParameter("@totalCount", SqlDbType.Int,4),
					};
                parameters[0].Value = i_user_id;
                parameters[1].Value = i_page_index;
                parameters[2].Value = i_page_size;
                parameters[3].Direction = ParameterDirection.Output;

                DataSet ds = DbHelperSQL.RunProcedure("proc_UserReview_Get", parameters, "ds");
                if (!int.TryParse(parameters[3].Value.ToString(), out o_total_count))
                    o_total_count = 0;
                return ds;
            }
            catch (Exception ex)
            {
                throw new DataException("获取用户评论异常", ex);
            }
        }

        /// <summary>
        /// 删除用户评论
        /// </summary>
        /// <param name="i_user_id"></param>
        /// <param name="i_review_id"></param>
        /// <returns></returns>
        public int DeleteUserReview(long i_user_id, long i_review_id)
        {
            try
            {
                int rowsAffected;
                SqlParameter[] parameters = {
					new SqlParameter("@userId", SqlDbType.BigInt,8),
					new SqlParameter("@reviewId", SqlDbType.BigInt,8)
					
                };

                parameters[0].Value = i_user_id;
                parameters[1].Value = i_review_id;

                DbHelperSQL.RunProcedure("proc_UserReview_Delete", parameters, out rowsAffected);
                return rowsAffected;
            }
            catch (Exception ex)
            {
                throw new DataException("删除用户评论", ex);
            }
        }

        /// <summary>
        /// 更新用户资料
        /// </summary>
        /// <param name="i_user_id"></param>
        /// <param name="i_email"></param>
        /// <param name="i_password"></param>
        /// <returns></returns>
        public int UpdateUser(long i_user_id, string i_email, string i_password)
        {
            try
            {
                int rowsAffected;
                SqlParameter[] parameters = {
					new SqlParameter("@userId", SqlDbType.BigInt,8),
					new SqlParameter("@email", SqlDbType.NVarChar,50),
                    new SqlParameter("@password", SqlDbType.NVarChar,50)
					
                };

                parameters[0].Value = i_user_id;
                parameters[1].Value = i_email;
                parameters[2].Value = i_password;
                DbHelperSQL.RunProcedure("proc_tbUser_Update", parameters, out rowsAffected);
                return rowsAffected;
            }
            catch (Exception ex)
            {
                throw new DataException("更新用户资料", ex);
            }
        }

        /// <summary>
        /// 添加联系我们信息
        /// </summary>
        /// <param name="i_user_id"></param>
        /// <param name="i_contact_type"></param>
        /// <param name="i_content"></param>
        /// <param name="i_telephone"></param>
        /// <param name="i_qq"></param>
        /// <param name="i_email"></param>
        /// <returns></returns>
        public int AddContactUS(long i_user_id, int i_contact_type, string i_content, string i_telephone, string i_qq, string i_email)
        {
            try
            {
                int rowsAffected;
                SqlParameter[] parameters = {
					new SqlParameter("@userId", SqlDbType.BigInt,8),
					new SqlParameter("@contactType", SqlDbType.Int,4),
                    new SqlParameter("@content", SqlDbType.NVarChar,4000),
                    new SqlParameter("@telephone",SqlDbType.NVarChar,50),
                    new SqlParameter("@qq",SqlDbType.NVarChar,50),
                    new SqlParameter("@email",SqlDbType.NVarChar,50),
					
                };

                parameters[0].Value = i_user_id;
                parameters[1].Value = i_contact_type;
                parameters[2].Value = i_content;
                parameters[3].Value = i_telephone;
                parameters[4].Value = i_qq;
                parameters[5].Value = i_email;
                DbHelperSQL.RunProcedure("proc_tbContact_Add", parameters, out rowsAffected);
                return rowsAffected;
            }
            catch (Exception ex)
            {
                throw new DataException("更新用户资料", ex);
            }
        }
    }
}
